<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 配送方式ajax类
 * @package  ajax/backend/system
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysSend.class.php');

class ajax_sendType {

	public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1://配送方式列表
                    $this->sendTypeList();
                    break;

                case 2: //配送方式详情
                    $this->sendTypeDetail();
                    break;

                case 5://显示配送区域设置页面
                    $this->sendAreaList();
                    break;

                case 6://修改配送区域信息查询
                    $this->sendAreaData();
                    break;

                case 7: //查询配送区域省市区详细信息
                    $this->searchSendAreaList();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'change'))) {
                        returnJsonData('参数错误');
                    }

                    if ($_REQUEST['act'] == 'edit') {
                        $this->editSendType(); //新增、修改配送方式
                    } else if ($_REQUEST['act'] == 'del') {
                        $this->deleteSendType(); //删除配送方式
                    } else if ($_REQUEST['act'] == 'change') {
                        $this->changeSendTypeUseflg(); //配送方式启用/禁用
                    }

                    break;

                case 101:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'change'))) {
                        returnJsonData('参数错误');
                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->editSendArea(); //新增、修改配送区域

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteSendArea(); //删除配送区域

                    }
                    break;


                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

	}

    /**
     * 查询配送方式详情
     */
    private function sendTypeDetail() {

        if (!isset($_REQUEST['sendTypeID']) || !quanli_is_int($_REQUEST['sendTypeID'])) {
            returnJsonData('参数错误');
        }

        quanli_include('module/base/system/sysPayment.class.php');

        //实例配送方式类
        $sysSend = new sysSend();

        //实例支付方式类
        $sysPayment = new sysPayment();

        //查询配送方式
        $arg_array = array(
            'sendTypeID' => $_REQUEST['sendTypeID']
        );
        $send_type_array = $sysSend->searchSendType( $arg_array );

        if ($send_type_array) {

            $send_type_array = $send_type_array[0];

            //查询配送方式对应的支付方式类型
            $args_array = array(
                'sendTypeID' => $send_type_array['sendTypeID']
            );
            $pay_type_array = $sysPayment->searchPayTypeFromSendType( $args_array );
            if ($pay_type_array) {
                $send_type_array['pay'] = $pay_type_array;
            }

            returnJsonData('ok', 200, $send_type_array);

        } else {

            returnJsonData('配送方式信息没有找到', 404);

        }

    }

    /**
     * 查询配送区域省市区详细信息
     */
    private function searchSendAreaList() {

        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id']))
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );

        global $G_PGLOB;

        //实例化系统类
        $sysSend = new sysSend($G_PGLOB->DBCA);

        //查询配送区域省市区详细信息
        $arg_array = array(
            'send_region_id' => $_REQUEST['id']
        );
        $result_array = $sysSend->searchSendAreaList( $arg_array );

        if ($result_array)
            echo json_encode( array( 'code' => 200, 'message' => 'ok', 'data' => $result_array ) );
        else
            echo json_encode( array( 'code' => 404, 'message' => '区域信息没有找到' ) );

    }


    /**
     * 配送方式列表
     */
    private function sendTypeList () {

        global $G_PGLOB;

        quanli_include('module/base/system/sysPayment.class.php');

        //实例配送方式类
        $sysSend = new sysSend();
        //实例支付方式类
        $sysPayment = new sysPayment();

		$send_type_array = $sysSend->searchSendType( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($send_type_array) {

            foreach($send_type_array as &$send) {

                $sendTypeID = $send['sendTypeID'];

                $name_array = '';

                //查询配送方式对应的支付方式类型
                $args_array = array(
                    'sendTypeID' => $sendTypeID
                );
                $pay_type_array = $sysPayment->searchPayTypeFromSendType( $args_array );
                if ($pay_type_array) {
                    foreach($pay_type_array as &$pay_type) {
                        $name_array[] = $pay_type['pay_type_name'];
                    }
                }

                $operate_html = '';
                if ($G_PGLOB->has_permission(12)) {
                    $operate_html .= '<a href="javascript:void(0);" onclick="send.edit.open(' . $sendTypeID . ');">修改</a>&nbsp;&nbsp;';
                }
                $operate_html .= '<a href="javascript:void(0);" onclick="send.area.open(' . $sendTypeID . ');">区域运费</a>';

                $result_array['rows'][] = array(
                    'id' => $sendTypeID,
                    'cell' => array(
                        $operate_html,
                        $send['sendTypeName'],
                        $send['description'],
                        empty($name_array) ? '' : implode('，', $name_array),
                        '<a href="javascript:void(0);" onclick="send.change.check(' . $sendTypeID . ', ' . $send['useFlg'] . ');">' . $send['useFlgName'] . '</a>'
                    )
                );

            }

        }

        echo json_encode($result_array);

	}


    /**
     * 新增、修改配送方式
     */
    private function editSendType () {

        global $G_PGLOB;

        //实例配送方式类
		$sysSend = new sysSend();

        //新增、修改配送方式
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $sysSend->editSendType( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

	}

    /**
     * 删除配送方式
     */
    private function deleteSendType () {

        if (!isset($_REQUEST['delete_array'][0])) {
            returnJsonData('参数错误');
        }

        //实例配送方式类
        $sysSend = new sysSend();

        //删除配送方式
        $result_array = $sysSend->deleteSendType( $_REQUEST['delete_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 配送方式启用/禁用
     */
    private function changeSendTypeUseflg () {

        //实例配送方式类
        $sysSend = new sysSend();

        //配送方式启用/禁用
        $result_array = $sysSend->changeSendTypeUseflg( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 查询配送区域
     */
    private function sendAreaList () {

		global $G_PGLOB;

        //实例化系统类
		$sysSend = new sysSend( $G_PGLOB->DBCA );


        //查询配送区域
        $area_array = $sysSend->searchSendArea( $_REQUEST );


        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );


        if ($area_array) {

            foreach($area_array as &$area) {

                $send_region_id = $area['send_region_id'];

                $result_array['rows'][] = array(
                    'id' => $send_region_id,
                    'cell' => array(
                        '<a href="javascript:void(0);" onclick="area.add(' . $send_region_id . ');">修改</a>&nbsp;&nbsp;',
                        $send_region_id,
                        $area['send_region_name'],
                        $area['areaName'],
                        $area['freight'],
                        $area['ykg'],
                        $area['overweight_freight'],
                    )
                );
            }
        }
		
		echo json_encode($result_array);

	}

    /**
     * 修改配送区域信息查询
     */
    private function sendAreaData (){

        global $G_PGLOB;

        if(isset($_REQUEST['send_region_id']) && !empty($_REQUEST['send_region_id'])){

            //实例化系统类
            $sysSend = new sysSend( $G_PGLOB->DBCA );
            //保存/修改配送区域
            $_REQUEST['editor'] = $G_PGLOB->currentUserID;
            $result_array = $sysSend->searchSendArea( $_REQUEST );

            echo json_encode(array('code'=>200,'data'=>$result_array[0]));
        }else{
            echo json_encode(array('code'=>400,'message'=>'配送区域没找到，请刷新页面后重试'));
        }

    }

    /*
	 * 保存/修改配送区域
	 */
	private function editSendArea (){

        global $G_PGLOB;

        //实例化系统类
		$sysSend = new sysSend( $G_PGLOB->DBCA );

        //保存/修改配送区域
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
		$result_array = $sysSend->editSendArea( $_REQUEST );
		
		echo json_encode($result_array);

	}
	
    /**
     *删除配送区域
     */
    private function deleteSendArea () {

        if (!isset($_REQUEST['str']) || empty($_REQUEST['str']))
            die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );

        global $G_PGLOB;
		
        //实例化系统类
		$sysSend = new sysSend( $G_PGLOB->DBCA );

        //分解字符串
        $area_id_array = explode('_', $_REQUEST['str']);

        //删除配送区域
		$result_array = $sysSend->deleteSendArea( $area_id_array );
		
		echo json_encode($result_array);
	}


}